﻿using AutoMapper;
using KWT.DRG_DIP.DB;
using KWT.DRG_DIP.DO.Group.DRGGroup;
using KWT.DRG_DIP.DTO.Group;
using KWT.DRG_DIP.ISvc;
using KWT.DRG_DIP.PO.Basic;
using KWT.DRG_DIP.Svc.Group.DRGGroup;

namespace KWT.DRG_DIP.Svc
{
    public class DRGGrouperSvc : IDrgGrouperSvc
    {
        public IMapper _mapper { get; set; }
        public IPolicyEnumSvc policyEnumSvc { get; set; }
        public EFContext ef { get; set; }

        public OutGroupResult GetGroupInfo(OutGroupResult? groupResult, string tenantID)
        {
            if (groupResult == null)
                return groupResult;
            var para = ef.Set<Sys_DRG_Para>().SingleOrDefault(x => x.DRG_Code == groupResult.strResult);
            groupResult.strResultName = para == null ? "" : para.DRG_Name;
            return groupResult;
        }

        /// <summary>
        /// 获得ADRG 
        /// </summary>
        /// <param name="inputPara">分组参数</param>
        /// <param name="version">分组器版本</param>
        /// <returns></returns>
        public OutGroupResult GroupExecue(InGroupPara inGroupPara, string tenantID)
        {
            DRGGroupBase dRGGroupBase = new DRGGroupBase();
            var inputPara = _mapper.Map<DRGGroupInputPara>(inGroupPara);

            switch (inputPara.version)
            {
                default:
                    dRGGroupBase = new DRGGroup();
                    break;
            }
            #region 初始化规则字典
            //初始化规则字典
            dRGGroupBase.ruleMDCs = policyEnumSvc.GetRuleMDC(tenantID);
            dRGGroupBase.ruleADRGs = policyEnumSvc.GetRuleADRG(tenantID);
            dRGGroupBase.rule_DRGs = policyEnumSvc.GetRuleDRG(tenantID);

            dRGGroupBase.rule_MCCs = policyEnumSvc.GetRuleMCC(tenantID);
            dRGGroupBase.rule_CCs = policyEnumSvc.GetRuleCC(tenantID);
            dRGGroupBase.rule_Excludes = policyEnumSvc.GetRuleExclude(tenantID);
            dRGGroupBase.rule_notDiagnoses = policyEnumSvc.GetRuleNotDiagnose(tenantID);
            dRGGroupBase.rule_NotOperations = policyEnumSvc.GetRuleNotOperation(tenantID);

            #endregion

            var result = dRGGroupBase.GroupExecue(inputPara);
            return _mapper.Map<OutGroupResult>(result);
        }
    }
}
